package com.funo.logic.generate.code;

import com.funo.logic.generate.wrapper.ClassWrapper;
import com.funo.logic.generate.wrapper.MethodInvokeWrapper;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * MethodCodeSupport方法内部逻辑代码
 * @since 2024年7月25日15:53:05
 * @author cms
 */
public class MethodFieldCodeSupport extends AbstractCodeSupport implements CodeSupport{
    /**
     * 变量名
     */
    private String variableName;

    private ClassWrapper returnType;

    private List<ParamCodeSupport> parameterTypes;

    private List<String> commentList = new ArrayList<>();
    /**
     * 表达式
     */
    private String expression = "=";
    /**
     * 初始化值
     */
    private String initialization;
    /**
     * 调用的服务类
     */
    private MethodInvokeWrapper serviceWrapper;

    private List<ParamCodeSupport> paramCodeSupports = new LinkedList<>();

    @Override
    public String getName() {
        return "";
    }

    @Override
    public String generateCode() {
        for (String comment: commentList) {
            if (comment.startsWith("//")) {
                codeBuffer.append(comment);
            } else {
                codeBuffer.append("//").append(comment);
            }
            addNewLine();
        }
        if (returnType != null) {
            codeBuffer.append(returnType.getName());
        }
        addSpace();
        if (StringUtils.isNotEmpty(variableName)) {
            codeBuffer.append(variableName);

            addSpace();
            codeBuffer.append(expression);
            addSpace();
        }

        // 有变量值
        if (StringUtils.isNotEmpty(initialization)) {
            codeBuffer.append(initialization);
        }
        if (serviceWrapper != null) {
            codeBuffer.append(serviceWrapper.generateCode());
            addOpenParen();
            for (ParamCodeSupport paramCodeSupport: paramCodeSupports) {
                codeBuffer.append(paramCodeSupport.getName());
            }
            addCloseParen();
        }
        addSemicolon();
        return codeBuffer.toString();
    }

    public void setReturnType(ClassWrapper returnType) {
        this.returnType = returnType;
    }

    public void setVariableName(String variableName) {
        this.variableName = variableName;
    }

    public void setInitialization(String initialization) {
        this.initialization = initialization;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }

    public void setServiceWrapper(MethodInvokeWrapper serviceWrapper) {
        this.serviceWrapper = serviceWrapper;
    }

    public void addComment(String comment) {
        commentList.add(comment);
    }
    public void addParamCodeSupport(ParamCodeSupport paramCodeSupport) {
        if (paramCodeSupports.stream().noneMatch(p -> p.getName().equals(paramCodeSupport.getName()))) {
            paramCodeSupports.add(paramCodeSupport);
        }
    }
}
